﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Royaumes_Libres.Database;

namespace Royaumes_Libres
{
    /// <summary>
    /// Pour lister les buildings disponible à construire.
    /// </summary>
    /// <remarks>Patrick Chabot</remarks>
    public partial class ucBuilding : System.Web.UI.UserControl
    {
        protected cBuildings pBuilding;
        protected cRessource pRessource;
        protected cBuildingsBuilds pBuildingsBuilds;

        public cBuildings Building { set { this.pBuilding = value; } }
        public cRessource Ressource { set { this.pRessource = value; } }
        public cBuildingsBuilds BuildingsBuilds { set { this.pBuildingsBuilds = value; } }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.InitControl();
        }

        public void InitControl()
        {
            bool btnConstructEnable = true;
            this.imgBuilding.ImageUrl = this.pBuilding.ImgUrl;

            this.lblName.Text = cText.GetText(this.pBuilding.Name);
            this.lblTimber.Text = this.pBuilding.Timber.ToString();
            this.lblOre.Text = this.pBuilding.Ore.ToString();
            this.lblMana.Text = this.pBuilding.Mana.ToString();

            decimal timeWork = Math.Ceiling((decimal)this.pBuilding.WorkUnite / this.pBuildingsBuilds.WorkUnite);
            this.lblTimeLeft.Text = string.Format(cText.GetText("Hours"), timeWork);

            if (this.pBuildingsBuilds.RemainingWork > 0)
            {
                if (this.pBuildingsBuilds.PkBuilding == this.pBuilding.PkBuilding)
                {
                    decimal timeleft = Math.Ceiling((decimal)this.pBuildingsBuilds.RemainingWork / this.pBuildingsBuilds.WorkUnite);
                    this.msgConstruct.Text = string.Format(cText.GetText("RemainingTime"), timeleft);
                }
                else
                {
                    this.msgConstruct.Text = cText.GetText("WorkInProgress");
                }
                this.btnConstruct.Visible = false;
                this.msgConstruct.Visible = true;
            }
            else
            {
                if (this.pBuilding.Timber > this.pRessource.Timber)
                {
                    btnConstructEnable = false;
                    this.lblTimber.ForeColor = System.Drawing.Color.Red;
                }
                if (this.pBuilding.Ore > this.pRessource.Ore)
                {
                    btnConstructEnable = false;
                    this.lblOre.ForeColor = System.Drawing.Color.Red;
                }
                if (this.pBuilding.Mana > this.pRessource.Mana)
                {
                    btnConstructEnable = false;
                    this.lblMana.ForeColor = System.Drawing.Color.Red;
                }

                this.msgConstruct.Visible = false;
                this.btnConstruct.Text = cText.GetText("Build");
                this.btnConstruct.Visible = true;
                this.btnConstruct.Enabled = btnConstructEnable;
            }

            this.btnConstruct.Click += new EventHandler(btnConstruct_Click);
        }

        void btnConstruct_Click(object sender, EventArgs e)
        {
            this.pBuilding.StartBuild();
            Response.Redirect("userbuilding.aspx");
        }
    }
}